package com.jfirer.validator.spi;

import javax.validation.Configuration;
import javax.validation.ValidatorFactory;
import javax.validation.spi.BootstrapState;
import javax.validation.spi.ConfigurationState;
import javax.validation.spi.ValidationProvider;
import com.jfirer.validator.util.ReflectPropertyValueDescriptorFactory;
import com.jfirer.validator.validation.JfireValidatorConfiguration;
import com.jfirer.validator.validation.ValidatorFactoryImpl;
import com.jfirer.validator.validation.JfireValidatorConfiguration.JfireValidatorConfigurationState;

public class ValidatorProviderImpl implements ValidationProvider<JfireValidatorConfiguration>
{
    @Override
    public JfireValidatorConfiguration createSpecializedConfiguration(BootstrapState state)
    {
        return new JfireValidatorConfiguration(this);
    }
    
    @Override
    public Configuration<?> createGenericConfiguration(BootstrapState state)
    {
        return new JfireValidatorConfiguration(state.getValidationProviderResolver() == null ? state.getDefaultValidationProviderResolver() : state.getValidationProviderResolver());
    }
    
    @Override
    public ValidatorFactory buildValidatorFactory(ConfigurationState configurationState)
    {
        if (configurationState instanceof JfireValidatorConfigurationState)
        {
            ValidatorFactoryImpl validatorFactoryImpl = new ValidatorFactoryImpl(configurationState, ((JfireValidatorConfigurationState) configurationState).getPropertyValueDescriptorFactory());
            return validatorFactoryImpl;
        }
        else
        {
            ValidatorFactoryImpl validatorFactoryImpl = new ValidatorFactoryImpl(configurationState, new ReflectPropertyValueDescriptorFactory());
            return validatorFactoryImpl;
        }
    }
    
}
